package suncertify.controller;

import java.io.IOException;

import suncertify.db.Data;
import suncertify.db.RecordNotFoundException;
import suncertify.network.ServerHelper;
import suncertify.network.ServerInterface;
import suncertify.resources.App;

/**
 * Manages the user actions coming from the <code>BookGui</code> class.
 * @author Paul Wilhelm
 *
 */
public class BookController {
	
	private ServerInterface data;		//The data class to be used
	
	/**
	 * Constructor.  Creates a new <code>BookController</code> object and assigns the data object
	 * depending on whether the user is running in networked or alone mode.
	 */
	public BookController(){
		if (App.isNetworked){			//If networked mode
			data = ServerHelper.getRemoteDatabase();
		}
		else {							//If alone mode
			data = Data.getInstance();
		}
	}

	/**
	 * The actions to perform when the "Book" button is clicked.  Finds matching records based on
	 * name and location, then updates the record to show it booked.
	 * @param custNum - The number that was entered as the customer number
	 * @param name - The name of the hotel to book
	 * @param location - The location of the hotel to book
	 * @throws RecordNotFoundException
	 * @throws IOException
	 */
	public void bookBtnClick(String custNum, String name, String location)throws RecordNotFoundException, IOException{
		int[] matchingRecord = data.find(new String[] {name, location});	
		data.setRequestingName(name);
		data.setRequestingLocation(location);
		String[] field = new String[] {custNum};	
		data.update(matchingRecord[0], field);			
	}

}
